<?php 
$this->headTitle('Thêm Tin', 'append');
?>
<div class="dashboard-menu dashboard-admin">
    <h1>Thêm Tin</h1>
</div>
<div class="row placeholders">
    <div class="col-md-12">
        <div class="widget-big">
            <div class="search-result text-left">
                <?php
                    if(isset($this->mess)){
                        if($this->flag == 'success')
                            echo '<div class="bg-success">',$this->mess,'</div>';
                        elseif($this->flag == 'alert')
                            echo '<div class="bg-warning">',$this->mess,'</div>';
                        else
                            echo '<div class="bg-danger">',$this->mess,'</div>';
                    }
                ?>
                <?php //echo $this->form?>
                <form id="form" class="form-option" method="post" action="" enctype="multipart/form-data" name="form">
                    <dl class="zend_form">
                        <?php echo $this->form->id?>
                        <?php echo $this->form->title?>
                        <?php echo $this->form->createDate?>
                        <?php echo $this->form->startDate?>
                        <?php echo $this->form->endDate?>
                                                
                        <!--<dt><label>Ngày Tạo (*)</label></dt>
                        <dd><input type="text" name="createDate" class="datepicker2" value="<?php echo date("Y-m-d",time()-86400);?>" placeholder="dd/mm/yy"/></dd>
                        <dt><label>Ngày Bắt Đầu (*)</label></dt>
                        <dd><input type="text" name="startDate" class="datepicker2" value="<?php echo date("Y-m-d",time()-86400);?>" placeholder="dd/mm/yy"/></dd>
                        <dt><label>Ngày Kết Thúc (*)</label></dt>
                        <dd><input type="text" name="endDate" class="datepicker2" value="<?php echo date("Y-m-d",time()-86400);?>" placeholder="dd/mm/yy"/></dd>-->
                        
                        <?php echo $this->form->content?>
                        <?php echo $this->form->transaction?>
                        <?php echo $this->form->estate?>
                        <?php echo $this->form->area?>
                        <?php echo $this->form->price?>
                                                                                                
                        <dt><label>Đơn Vị (*)</label></dt>
                        <dd><select name="unit" id="unit">
                            <option value="-1">Chọn Đơn Vị</option>
                        </select></dd> 
                        
                        <?php echo $this->form->total?>
                        
                        <dt id="otherInfo"><label>Thông tin khác</label></dt>
                        <dd id="otherInfo">
                            <div class="option-wrapper">
                                <div class="fieldset">
                                    <div class="legend"></div>
                                    <div class="fieldset-content">
                                    <ul id="listother">
                                        <?php foreach($this->otherInfo as $row){?>
                                            <li><label><?php echo $row['name']?></label><input type='text' class='resourceIdAdmin' name="<?php echo $row['id']?>" value=''/></li>                                         
                                        <?php }?>
                                        <li><label>Hướng nhà</label>
                                            <select name="5" style="width: 180px;">
                                            <?php foreach($this->car as $row){?>
                                            <option value="<?php echo $row?>"><?php echo $row;?></option>
                                            <?php }?>
                                            </select></li>
                                        <li><label>Hướng ban công</label>
                                            <select name="6" style="width: 180px;">
                                            <?php foreach($this->car as $row){?><option value="<?php echo $row?>"><?php echo $row;?></option><?php }?>
                                            </select></li>
                                    <div class="clearfix"></div>
                                    </ul>
                                    </div>
                                </div>
                            </div>
                        </dd>                                                
                        
                        <dt id="otherUtility"><label>Tiện ích khác</label></dt>
                        <dd id="otherUtility">
                            <div class="option-wrapper">
                                <div class="fieldset">
                                    <div class="legend"><input type="checkbox" name="otherUtility" id="check_all_resource_default"/><label>Chọn</label></div>
                                    <div class="fieldset-content">
                                    <ul>
                                    <?php 
                                    
                                        foreach ($this->other as $info){
                                            if($info['groupInfo'] == 'otherUtility'){
                                    ?>
                                            <li><?php echo $this->form->$info['id']?><label><?php echo $this->form->$info['id']->getLabel()?></label></li><!--<input type='checkbox' class='resourceIdAdmin' name='utility[]' value='{$info['id']}'/>-->
                                    <?php
                                                
                                            }
                                        }
                                    ?>                                                                        
                                    <div class="clearfix"></div>
                                    </ul>
                                    </div>
                                </div>
                            </div>
                        </dd>
                        <dt><label>Tỉnh/thành phố</label></dt>
                        <dd><select name="province" id="province">
                            <option value="-1">-- Tỉnh/thành phố --</option>   
                            <?php foreach($this->province as $value){?>
                            <option value="<?php echo $value['provinceid'];?>"><?php echo $value['name'];?></option>
                            <?php }?>
                        </select></dd>
                        <dt><label>Quận/Huyện</label></dt>
                        <dd><select name="district" id="district">
                            <option value="-1">-- Quận/huyện--</option>   
                        </select></dd>
                        <dt><label>Phường/Xã</label></dt>
                        <dd><select name="ward" id="ward">
                            <option value="-1">-- Phường/xã--</option>   
                        </select></dd>
                        <dt><label>Địa chỉ (*)</label></dt>
                        <dd><input type="text" id="address" name="address" value=""/></dd>
                        <dt><label>Dự án</label></dt>
                        <dd><select name="project" id="project">
                            <option value="-1">-- Dự Án--</option>   
                            <?php foreach($this->project as $value){?>
                            <option value="<?php echo $value['projectId'];?>"><?php echo $value['projectName'];?></option>
                            <?php }?>
                        </select></dd>                            
                        <?php echo $this->form->contact?>                        
                        <?php echo $this->form->mobile?>
                        <?php echo $this->form->email?>
                        <?php echo $this->form->phoneContact?>
                                                                        
                        <dt><label>Status</label></dt>
                        <dd><select name="status" id="status">
                            <option value="1">Hoạt Động</option>
                            <option value="0">Tạm Khóa</option>
                        
                        </select></dd> 
                        <dd id="submit-element">
                            <?php echo $this->form->submit?>
                            <?php echo $this->form->reset?>
                            <!--<input value="Thêm mới" class="button" type="submit" />
                            <input value="Reset" class="button" type="reset"/>-->
                        </dd>
                    </dl>
                </form>
            </div>
        </div>        
    </div>
</div></select><!--cho thue 0, bán 1 !-->
<script type="text/javascript">CKEDITOR.replace('content');</script>
<script type="text/javascript">
    $(document).ready(function(){
        $('#estate option[value!="-1"]').remove();
    });
</script>
<script type="text/javascript">    
    $("#province").change(function(){
        $('select#ward option[value!="-1"]').remove();
        province=$(this).val();
        $.ajax({
            type: "POST",
            url: "/admin/location/getdistrict/province/"+province,                                               
            dataType: 'json',                
            success: function(data) {
                $('select#district option[value!="-1"]').remove();
                for(var key in data){                        
                    $('select#district').append(new Option(data[key],key));
                }                                    
            },
        });
    }); 
    $("#district").change(function(){
         district=$(this).val();         
         $.ajax({
                type: "POST",
                url: "/admin/location/getward/district/"+district,                                               
                dataType: 'json',                
                success: function(data) {
                    $('select#ward option[value!="-1"]').remove();
                    for(var key in data){   
                        $('select#ward').append(new Option(data[key],key));
                    }                                    
                },
            });
    });
    $("#ward").change(function(){
         wardName=$("#ward :selected").text();
         wardVal=$(this).val();
         type=wardVal.split('_');
         provinceName=$(this).parent().parent().find('#province option:selected').text();
         districtName=$(this).parent().parent().find('#district option:selected').text();
         $('#address').val(' '+type[1]+' '+wardName+', '+districtName+', '+provinceName);
    });
    $("#transaction").change(function(){     
        var tran=$(this).val();        
        $.ajax({
            type: "POST",
            url: "/admin/estate/getestate/transaction/"+tran,                                               
            dataType: 'json',                
            success: function(data) {
                $('select#estate option[value!="-1"]').remove();
                for(var key in data){                     
                    $('select#estate').append(new Option(data[key],key));
                }                                    
            },            
        });
        $.ajax({
            type: "POST",
            url: "/admin/estate/getunit/transaction/"+tran,                                               
            dataType: 'json',                
            success: function(data) {
                $('select#unit option[value!="-1"]').remove();
                for(var key in data){                       
                    $('select#unit').append(new Option(data[key],tran+key));
                }                                    
            },
        });         
    });
    $('#estate').change(function(){
        var estate=$(this).val();                        
        $.ajax({
            type: "POST",
            url: "/admin/estate/getutility/estate/"+estate,                                                  
            dataType: 'json',                           
            success: function(data) {
                alert(JSON.stringify(data));                                                                            
                for(var key in data){
                    if(key=='otherInfoShow'){
                        for(var key in data['otherInfoShow']){
                             $('[name='+data['otherInfoShow'][key]+']').parent().attr('style','') ;                                                                 
                        }                                                        
                    }
                    if(key=='otherInfoHide'){
                        for(var key in data['otherInfoHide']){
                             $('[name='+data['otherInfoHide'][key]+']').parent().attr('style','display: none;') ;                                                                 
                        }                                                        
                    }
                    if(key=='otherUtilityShow'){
                        for(var key in data['otherUtilityShow']){
                             $('#'+data['otherUtilityShow'][key]).parent().attr('style','') ;                                                                 
                        }                                                        
                    }
                    if(key=='otherUtilityHide'){
                        for(var key in data['otherUtilityHide']){
                             $('#'+data['otherUtilityHide'][key]).parent().attr('style','display: none;') ;                                                                 
                        }                                                        
                    }                                                                        
                }                
            }                          
        });                                             
    });
    

    
$(document).ready(function(){                         
    $("#area").keyup(function(){
        area=$(this).val();
        price = $(this).parent().parent().find('#price').val();
        unit=$(this).parent().parent().find('#unit option:selected').val();
        after=$(this).parent().parent().find('#unit option:selected').text();
        if(area && price && unit){
            if(unit=='sale1' || unit=='sale2' || unit=='rent1' || unit=='rent2'){
            $(this).parent().parent().find('#total').val(price+' '+after);
            }
            else if(unit=='sale3'){
                result=parseInt(area)*parseInt(price);
                $(this).parent().parent().find('#total').val(parseFloat(result/10) +' Triệu');
            }
            else if(unit=='sale4'){
                result=parseInt(area)*parseInt(price);
                $(this).parent().parent().find('#total').val(result+' Triệu');
            }
            else if(unit='rent3'){
                result=parseInt(area)*parseInt(price);
                $(this).parent().parent().find('#total').val(parseFloat(result/10)+' Triệu/Tháng');
            }
            else{
                result=parseInt(area)*parseInt(price);
                $(this).parent().parent().find('#total').val(parseFloat(result)+' Triệu/Tháng');
            }
        }
    });
     $("#price").keyup(function(){
         price=$(this).val();
         area = $(this).parent().parent().find('#area').val();
         unit=$(this).parent().parent().find('#unit option:selected').val();
         after=$(this).parent().parent().find('#unit option:selected').text();
         if(area && price && unit){
             if(unit=='sale1' || unit=='sale2' || unit=='rent1' || unit=='rent2'){
                 $(this).parent().parent().find('#total').val(price+' '+after);
             }
             else if(unit=='sale3'){
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(parseFloat(result/10) +' Triệu');
             }
             else if(unit=='sale4'){
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(result+' Triệu');
             }
             else if(unit='rent3'){
                  result=parseInt(area)*parseInt(price);
                  $(this).parent().parent().find('#total').val(parseFloat(result/10)+' Triệu/Tháng');
             }
             else{
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(parseFloat(result)+' Triệu/Tháng');
             }
         }
     });
     $("#unit").change(function(){
        var unit=$(this).val();
        area = $(this).parent().parent().find('#area').val();
        price = $(this).parent().parent().find('#price').val();
        after=$(this).parent().parent().find('#unit option:selected').text();
        if(unit=='rent5'|| unit =='sale5'){
            $(this).parent().parent().find('#total').val('');
            document.getElementById("total").disabled = true;
        }else{
            document.getElementById("total").disabled = false;
        } 
        if(area && price && unit){
             if(unit=='sale1' || unit=='sale2' || unit=='rent1' || unit=='rent2'){
                 $(this).parent().parent().find('#total').val(price+' '+after);
             }
             else if(unit=='sale3'){
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(parseFloat(result/10) +' Triệu');
             }
             else if(unit=='sale4'){
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(result+' Triệu');
             }
             else if(unit='rent3'){
                  result=parseInt(area)*parseInt(price);
                  $(this).parent().parent().find('#total').val(parseFloat(result/10)+' Triệu/Tháng');
             }
             else{
                 result=parseInt(area)*parseInt(price);
                 $(this).parent().parent().find('#total').val(parseFloat(result)+' Triệu/Tháng');
             }
         }
     });            
});    
</script>